/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package phan.connection;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
//import javax.mail.internet.adressexception;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.*;

/**
 *
 * @author bailleurs services
 */
public class ContactMailServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

    public ContactMailServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
        //String username = request.getParameter("user").toString();
        String _usermail = request.getParameter("UserEmail").toString();
        String _question = request.getParameter("question").toString();
        System.out.println(_usermail);
        System.out.println(_question);



        boolean check = false;
        MySQLDBConnector sendMail = new MySQLDBConnector();
        ConnuLettre connu = new ConnuLettre();
        String subject = connu.getsubject(5);
        String _adminmail = connu.addmin();
        String host = connu.getMailHost();
        check = sendMail.sendMail(host, _usermail, _adminmail, subject, _question);

        // send ( host, from, to, subject, message );


        if (check) {
            //request.setAttribute ("user",);
            RequestDispatcher disp;
            disp = request.getRequestDispatcher("/contacterStep2.jsp");
            disp.forward(request, response);
        } else {
            request.setAttribute("isValide", "no");
            RequestDispatcher disp;
            disp = request.getRequestDispatcher("/retry.jsp");
            disp.forward(request, response);
        }
    // send ( host, from, to, subject, message );
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
